/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.rest;

import cn.hutool.core.lang.UUID;
import cn.hutool.core.map.MapUtil;
import me.zhengjie.annotation.Log;
import me.zhengjie.constant.ConstantUtils;
import me.zhengjie.modules.system.domain.IdentityItem;
import me.zhengjie.modules.system.service.IdentityCourseService;
import me.zhengjie.modules.system.service.IdentityItemService;
import me.zhengjie.modules.system.service.dto.*;
import me.zhengjie.utils.DateUtil;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://el-admin.vip
* @author 王宇
* @date 2023-09-24
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "身份产品管理")
@RequestMapping("/api/identityItem")
public class IdentityItemController {

    private final IdentityItemService identityItemService;

    @Autowired
    private IdentityCourseService identityCourseService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('identityItem:list')")
    public void download(HttpServletResponse response, IdentityItemQueryCriteria criteria) throws IOException {
        identityItemService.download(identityItemService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询身份产品")
    @ApiOperation("查询身份产品")
    //@PreAuthorize("@el.check('identityItem:list')")
    public ResponseEntity<Object> query(IdentityItemQueryCriteria criteria, Pageable pageable){
        Map<String, Object> map = identityItemService.queryAll(criteria, pageable);
        if (MapUtil.isNotEmpty(map) && map.get("content") != null) {
            List<IdentityItemDto> contentList = (List<IdentityItemDto>) map.get("content");
            contentList.parallelStream().forEach(s -> {
                IdentityCourseQueryCriteria courseQueryCriteria = new IdentityCourseQueryCriteria();
                courseQueryCriteria.setIdentityNo(s.getItemNo());
                List<IdentityCourseDto> courseItemDtos = identityCourseService.queryAll(courseQueryCriteria);
                s.setCourseList(courseItemDtos);
            });
        }
        return new ResponseEntity<>(map,HttpStatus.OK);
    }

    @PostMapping
    @Log("新增身份产品")
    @ApiOperation("新增身份产品")
    @PreAuthorize("@el.check('identityItem:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody IdentityItem resources){
        resources.setStatus(ConstantUtils.Valid);
        resources.setCreateby(SecurityUtils.getCurrentUsername());
        resources.setCreateTime(DateUtil.TimestampNow());
        resources.setItemNo(UUID.fastUUID().toString(true));
        return new ResponseEntity<>(identityItemService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改身份产品")
    @ApiOperation("修改身份产品")
    @PreAuthorize("@el.check('identityItem:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody IdentityItem resources){
        resources.setUpdateby(SecurityUtils.getCurrentUsername());
        resources.setUpdateTime(DateUtil.TimestampNow());
        identityItemService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除身份产品")
    @ApiOperation("删除身份产品")
    @PreAuthorize("@el.check('identityItem:del')")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody Long[] ids) {
        identityItemService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}